*********************************************************************************
/*    Create scatter plot for revenue, cost, and revenue/cost by income		*/
*********************************************************************************
clear all

* File paths
local datapath "${mvpf_audits}/data/cleaned" 
local graphpath "${graphpath}" 

* Import data 
// use "`datapath'/audit_tab_prim_inc_${min_yr}_${max_yr}.dta", clear

* Waterfall chart to produce
local 1 = 2
local useloop = `1' // =1 for operational, =2 for corr, =3 both

// * Groups of variables
// #delimit ;
// local rev_vars = "rev_tot rev_appl rev_cnsl rev_exam
// 				  rev_coll_notice rev_coll"
// 				  ;
//			
// local cost_vars = "costs_coll costs_exam costs_appl costs_tot
// 				labor_costs_exam${step} labor_costs_appl${step} labor_costs_coll
// 				labor_costs_cnsl${step} oh_costs_exam oh_costs_appl oh_costs_coll
// 				costs_notices costs_acs
// 				costs_labor costs_labor_future costs_oh" 
// 				;
// #delimit cr 
//
// * Variables of interest 
// local vars `cost_vars' `rev_vars'

* Version of fig to produce 
global version  ${version} // paper or slides

* Other formatting
if "${version}" == "slides" {
	global title "title(" ", size(vhuge))"
	global img wmf
	graph set window fontface "Arial"
	global title "title(" ", size(vhuge))"
	global axis_size "" //xsize(6.5) ysize(4)" // makes figure wider
}

else if "${version}" == "paper" {
	global title "title("")"
	global img pdf
	graph set window fontface "LMRoman10-Regular"
	global title " "
	global axis_size " " // makes figure wider
}



* create waterfall for each exam type	

* Ops 
	if `useloop'== 1 { 
		local hetlist `""if exam_type_pooled==1" "'
		local hettexts `" "Operational" "'
		local hetlabs `""ops" "'
	}

* Corr
	if `useloop'== 2 { 
		local hetlist `""if exam_type_pooled==3" "'
		local hettexts `" "Correspondence" "'
		local hetlabs `""corr" "'
	}
	
* create waterfall for all audits
	if `useloop'== 3 { 
		local hetlist `""if exam_type_pooled==1" "if exam_type_pooled==3""'
		local hettexts `" "Operational" "Correspondence""'
		local hetlabs `""ops" "corr""'
	}	
*	
/*------------------------------------------------------------------------------
Figure: Total revenue and costs by income percentile 
------------------------------------------------------------------------------*/
local j=1
// 	keep if exam_type_pooled==3
// 	keep rev_tot_disc ${inc}_pctl costs_tot_future rc_future_disc exam_type_pooled
// 	* Keep ops or corr audits
	use "${mvpf_audits}/data/figures_rep/ap3_c_d.dta", clear
foreach h in `hetlist' {
	local hetlab: word `j' of `hetlabs'
	local hettext: word `j' of `hettexts'
	di "`h'" "`hetlab'"	
	
// preserve

	
	* set y-axis label, title, and figure name
	local ylab = "Dollars per Audit ($)"
	lab val ${inc}_pctl ${inc}_pctl_label
	
	* xtitle
	if "${inc}"=="agi" global xtitle = "AGI percentile group"
	if "${inc}"=="tpi" global xtitle = "TPI percentile group"

	* Preferences 
	local symbsize small
	local textsize vsmall
	
	* Set fig range (y-axis) for each audit type (ops and corr)
	if "`hetlab'"== "ops" {
	    replace rev_tot_disc= 50000 if rev_tot_disc>50000
		local lb = 0
		local ub = 50000 // round(`max', 100)
		local int = 10000 // (`ub' - `lb')/5	
		global ylab "ylabel(0 "0" 10000 "10,000" 20000 "20,000" 30000 "30,000" 40000 "40,000" 50000 "95,500", nogrid format(%9.0fc))"
		}
			
	if "`hetlab'"== "corr" {
	    replace rev_tot_disc= 6000 if rev_tot_disc>6000
		local lb = 0
		local ub = 6000 // round(`max', 100)
		local int = 1000 // (`ub' - `lb')/5	
		global ylab "ylabel(0 "0" 1000 "1,000"  2000 "2,000" 3000 "3,000" 4000 "4,000" 5000 "5,000" 6000 " 21,000", nogrid format(%9.0fc))"
		} 
	
	

	*** Subgroup figure
	local fig_name = "scatter_costs_rev_${inc}"
		
	local lb_f : di %02.1f `lb'
	local ub_f : di %02.1f `ub'

	tw  (scatter rev_tot_disc ${inc}_pctl if inrange(${inc}_pctl, 1, 22), ///
		mcolor("0 114 178") msymbol(C) msize(`symbsize')) ///
		(scatter costs_tot_future ${inc}_pctl if inrange(${inc}_pctl, 1, 22) , ///
		mcolor(cranberry) msymbol(T) msize(`symbsize')) ///
		, ///
		yscale(r(`lb_f' `ub_f')) ///
		ytitle("`ylab'") xtitle("${xtitle}")   ///
		legend(order(1 2 )  lab(1 "Revenue") lab(2 "Total costs") ///
		ring(0) pos(11) col(1) symxsize(*3.5) region(lcolor(gs11))) ///
		///legend(off) ///
		${title} ${axis_size} ${ylab} ///
		xscale(r(1 22))  ///
		xlabel(1(1)22, value labsize(small) tl(.5) nogrid angle(45) )  
	graph export "`graphpath'/`fig_name'_`hetlab'.wmf", as(wmf) replace
// restore


/*------------------------------------------------------------------------------
Figure: Revenue/Cost by income percentile 
------------------------------------------------------------------------------*/	
// preserve
	* Keep ops or corr audits
// 	keep `h'
	
	* set y-axis label, title, and figure name
	local ylab = "Audit Revenue / Total Costs"
	lab val ${inc}_pctl ${inc}_pctl_label

	* Preferences 
	local symbsize small
	local textsize vsmall

	** Subgroup figure
	local fig_name = "scatter_rc_${inc}"
	
		* Set fig range (y-axis) for each audit type (ops and corr)
	if "`hetlab'"== "ops" {
		local lb = 0
		local ub = 7 // round(`max', 100)
		local int = 1 // (`ub' - `lb')/5	
		}
			
	if "`hetlab'"== "corr" {
		local lb = 0
		local ub = 12 // round(`max', 100)
		local int = 2 // (`ub' - `lb')/5	
		} 
		
	local lb_f : di %02.1f `lb'
	local ub_f : di %02.1f `ub'
	
	** xtitle
	if "`inc'"=="agi" global xtitle = "AGI percentile group"
	if "`inc'"=="tpi" global xtitle = "TPI percentile group"
	
	** Add Arrows pointing to markers at 99+
	summ rc_future_disc `h' & ${inc}_pctl==22 
	local rc_999: di %4.1f r(mean)
	
	summ rc_future_disc `h' & ${inc}_pctl==21 
	local rc_99: di %4.1f r(mean)

	
	** Rev/Labor Costs
	tw (scatter rc_future_disc ${inc}_pctl if inrange(${inc}_pctl, 1, 22) ,  ///
		 mcolor("${rc_col}") msymbol(S) msize(medsmall)) ///
		, ///
		yline(1, lw(thin) lp(dash) lc(gs11)) ///
		ylabel(`lb'(`int')`ub', nogrid format(%9.0fc)) yscale(r(`lb' `ub')) ///
		ytitle("`ylab'") xtitle("${xtitle}")   ///
		legend(off) ///
		${title} ${axis_size} ///
		xscale(r(1 22))  ///
		xlabel(1(1)22, value labsize(small) tl(.5) nogrid angle(45) )
	graph export "`graphpath'/`fig_name'_`hetlab'.wmf", as(wmf) replace
}
